package com.tang.managementdemo.controller;

import com.tang.managementdemo.pojo.*;
import com.tang.managementdemo.service.EmpService;
import com.tang.managementdemo.anno.LogOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;


//    @RequestMapping("/emp")
//    public Result list() {
//        List<Employ> all = empService.findAll();
//        return Result.success(all);
////        return empService.findAll();
//    }

    //查询员工信息
    @GetMapping
    public Result getAll(EmpQueryParam empQueryParam){
//        log.info("查询员工信息：page={},pagesize={}",page,pageSize);
        PageResult page1 = empService.page(empQueryParam);
        return Result.success(page1);
    }

    //根据Id查询员工信息
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("根据Id查询员工信息：{}",id);
        Employ byId = empService.getById(id);
        return Result.success(byId);
    }

    //查询全部员工
    @GetMapping("/list")
    public Result list(){
        List<Employ> all = empService.findAll();
        return Result.success(all);
    }

    //添加员工信息
    @PostMapping
    public Result add(@RequestBody Employ employ){
        log.info("新增员工：{}",employ);
        empService.save(employ);
        return Result.success();
    }

    //更新员工信息
    @PutMapping
    public Result update(@RequestBody Employ employ){
        log.info("更新员工：{}",employ);
        empService.save(employ);
        return Result.success();
    }
    //删除员工信息
    @LogOperation
    @DeleteMapping
    public Result deleteById(@RequestParam List<Integer> ids){
        log.info("删除员工：{}",ids);
        empService.deleteById(ids);
        return Result.success();
    }

}

